/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Operation;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * 
 */
public class OperationQualifiersSection extends PropertyViewSection {

	/**
	 * 
	 */
	private Button isAbstract, isLeaf, isQuery, isStatic, isUnique;
	
	/**
	 * 
	 */
	private SelectionListener isAbstractListener, isLeafListener, isQueryListener, isStaticListener, isUniqueListener;


	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {

		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory()
		.createFlatFormComposite(parent);
		FormData data;

		isAbstract = getWidgetFactory().createButton(composite, "isAbstract", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		isAbstract.setLayoutData(data);

		isAbstract.addSelectionListener(isAbstractListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Operation)getElement()).setIsAbstract(isAbstract.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		isLeaf = getWidgetFactory().createButton(composite, "isLeaf", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isAbstract, 20);
		data.top = new FormAttachment(0, 0);
		isLeaf.setLayoutData(data);

		isLeaf.addSelectionListener(isLeafListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Operation)getElement()).setIsLeaf(isLeaf.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		isQuery = getWidgetFactory().createButton(composite, "isQuery", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isLeaf, 20);
		data.top = new FormAttachment(0, 0);
		isQuery.setLayoutData(data);

		isQuery.addSelectionListener(isQueryListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Operation)getElement()).setIsQuery(isQuery.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});


		isStatic = getWidgetFactory().createButton(composite, "isStatic", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isQuery, 20);
		data.top = new FormAttachment(0, 0);
		isStatic.setLayoutData(data);

		isStatic.addSelectionListener(isStaticListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Operation)getElement()).setIsStatic(isStatic.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		isUnique = getWidgetFactory().createButton(composite, "isUnique", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isStatic, 20);
		data.top = new FormAttachment(0, 0);
		isUnique.setLayoutData(data);

		isUnique.addSelectionListener(isUniqueListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Operation)getElement()).setIsUnique(isUnique.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});


		CLabel derivedLabel = getWidgetFactory().createCLabel(composite, "Qualifiers:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(isAbstract,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(isAbstract, 0, SWT.CENTER);
		derivedLabel.setLayoutData(data);

	}

	/**
	 * 
	 */
	@Override
	public void refresh() {
		if( !isAbstract.isDisposed() &&! isLeaf.isDisposed() && !isQuery.isDisposed()&& !isStatic.isDisposed() && !isUnique.isDisposed()){
			if(getElement() instanceof Operation){
				isAbstract.setSelection(((Operation)getElement()).isAbstract());
				isLeaf.setSelection(((Operation)getElement()).isLeaf());
				isQuery.setSelection(((Operation)getElement()).isQuery());
				isStatic.setSelection(((Operation)getElement()).isStatic());
				isUnique.setSelection(((Operation)getElement()).isUnique());
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(isAbstract!=null && !isAbstract.isDisposed())
			isAbstract.removeSelectionListener(isAbstractListener);
		if(isLeaf!=null && !isLeaf.isDisposed())
			isLeaf.removeSelectionListener(isLeafListener);
		if(isQuery!=null && !isQuery.isDisposed())
			isQuery.removeSelectionListener(isQueryListener);
		if(isStatic!=null && !isStatic.isDisposed())
			isStatic.removeSelectionListener(isStaticListener);
		if(isUnique!=null && !isUnique.isDisposed())
			isUnique.removeSelectionListener(isUniqueListener);
	}

}

